#!/bin/bash

# Quick build script - Generate .exe installer with one click
# Usage: bash quick-build.sh

echo ""
echo "========================================"
echo "  ELM Project Packaging Tool"
echo "========================================"
echo ""

# Check Node.js
echo "[1/5] Checking Node.js..."
if ! command -v node &> /dev/null; then
    echo "ERROR: Node.js not found"
    echo "Please download and install Node.js from https://nodejs.org"
    exit 1
fi
echo "OK - Node.js installed: $(node --version)"
echo ""
sleep 2

# Check npm
echo "[2/5] Checking npm..."
if ! command -v npm &> /dev/null; then
    echo "ERROR: npm not found"
    exit 1
fi
echo "OK - npm installed: $(npm --version)"
echo ""
sleep 2

# Install dependencies
echo "[3/5] Installing dependencies..."
if [ ! -d "node_modules" ]; then
    echo "Downloading packages, please wait..."
    npm install
    if [ $? -ne 0 ]; then
        echo "ERROR: Failed to install dependencies"
        exit 1
    fi
else
    echo "OK - Dependencies already exist, skipping installation"
fi
echo ""
sleep 2

# Generate zip archive
echo "[4/5] Generating project archive..."
node build-installer.js
if [ $? -ne 0 ]; then
    echo "ERROR: Failed to generate archive"
    exit 1
fi
echo ""
sleep 2

# Generate .exe installer
echo "[5/5] Generating .exe installer..."
echo "This may take 2-5 minutes, please wait..."
npm run build:win
if [ $? -ne 0 ]; then
    echo "ERROR: Failed to generate .exe"
    exit 1
fi
echo ""
sleep 2

echo ""
echo "========================================"
echo "  SUCCESS - Packaging complete!"
echo "========================================"
echo ""
echo "Generated files location: dist/"
echo ""
echo "File list:"
echo "  - ELM Project Launcher Setup 1.0.0.exe  (Installer)"
echo "  - ELM Project Launcher 1.0.0.exe        (Portable)"
echo "  - elm-project.zip                       (Project archive)"
echo ""
echo "Next steps:"
echo "  1. Double-click Setup .exe to install"
echo "  2. Or run portable .exe directly"
echo "  3. Click 'Start All' button to launch services"
echo ""
